package mo.tags.hash;

import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

public class L17 {

    private static Map<Character, List<Character>> num2chars = new HashMap<>();

    static {
        num2chars.put('2', Arrays.asList('a', 'b', 'c'));
        num2chars.put('3', Arrays.asList('d', 'e', 'f'));
        num2chars.put('4', Arrays.asList('g', 'h', 'i'));
        num2chars.put('5', Arrays.asList('j', 'k', 'l'));
        num2chars.put('6', Arrays.asList('m', 'n', 'o'));
        num2chars.put('7', Arrays.asList('p', 'q', 'r', 's'));
        num2chars.put('8', Arrays.asList('t', 'u', 'v'));
        num2chars.put('9', Arrays.asList('w', 'x', 'y', 'z'));
    }

    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        for (int i = 0; i < digits.length(); i++) {
            List<Character> chars = num2chars.get(digits.charAt(i));
            if (CollectionUtils.isEmpty(res)) {
                res = chars.stream().map(String::valueOf).collect(Collectors.toList());
            } else {
                List<String> curs = new ArrayList<>();
                for (int j = 0; j < res.size(); j++) {
                    for (int k = 0; k < chars.size(); k++) {
                        curs.add(res.get(j) + chars.get(k));
                    }
                }
                res = curs;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        L17 test = new L17();
        System.out.println(test.letterCombinations("23"));
    }
}
